﻿/*!	@file	r_input_device_controll_in.cpp
	@brief	入力装置
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "../r_debug.h"
#include "r_input_device_controll_in.h"

#include "../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace input{
// >>>>>>>>>> RTTI >>>>>>>>>>
RRTTI_REFLECTION_IMPLEMENT ( r::input::DeviceControllIn, r::input::DeviceControll )

// <<<<<<<<<< RTTI <<<<<<<<<<

// --------------------------------------------------

		DeviceControllIn :: DeviceControllIn()
			:
			DeviceControll()
		{
		}

		DeviceControllIn :: ~DeviceControllIn()
		{
		}


		//! プラットフォーム共通の初期化
		/*!
		*/
		void DeviceControllIn :: init ( void )
		{
			keyAllPress = 0;
			keyAllPush = 0;
			keyAllRepeat = 0;
			for ( r_si32 i = 0; i < DeviceControllKeys_Num; ++i ) {
				key[i] = false;
				keyCount[i] = 0;
			}
			for ( r_si32 i = 0; i < DeviceControllKeyAnalog_Num; ++i ) {
				analog[i] = 0;
			}
			for ( r_si32 i = 0; i < DeviceControllKeyAnalog2D_Num; ++i ) {
				analog2D[i].x = analog2D[i].y = 0;
			}
			screen.x = screen.y = 0;
		}
		//! キー入力開始
		/*!
		*/
		void DeviceControllIn :: beginInput ( void )
		{
			isConnected = false;
			keyAllPress = 0;
			keyAllPush = 0;
			keyAllRepeat = 0;
			keyAllRelease = 0;
			for ( r_si32 i = 0; i < DeviceControllKeys_Num; ++i ) {
				key[i] = false;
			}
			for ( r_si32 i = 0; i < DeviceControllKeyAnalog_Num; ++i ) {
				analog[i] = 0;
			}
			for ( r_si32 i = 0; i < DeviceControllKeyAnalog2D_Num; ++i ) {
				analog2D[i].x = analog2D[i].y = 0;
			}
		}
		//! キー入力終了、入力状況を基に簡単に取り出し可能なキー情報を作成
		/*!
		*/
		void DeviceControllIn :: endInput ( void )
		{
			Params::instance inputPrams;
			// キーの入力状態
			for ( r_si32 i = 0; i < DeviceControllKeys_Num; ++i ) {
				// press成立
				if ( key[i] != false ) {
					keyAllPress |= ( 1 << i );
					// push成立
					if ( keyCount[i] == 0 ) {
						keyAllPush |= ( 1 << i );
					}
					// repeat成立
					else if ( ( keyCount[i] >= inputPrams->repeateInFrames ) && ( ( ( keyCount[i] - inputPrams->repeateInFrames ) % inputPrams->repeateInterval ) == 0 ) ) {
						keyAllRepeat |= ( 1 << i );
					}
					++keyCount[i];
				} else {
					// release成立
					if ( keyCount[i] > 0 ) {
						keyAllRelease |= ( 1 << i );
					}
					keyCount[i] = 0;
				}
			}
		}
		//! デジタルキー入力
		/*!
		*/
		void DeviceControllIn :: setKey ( r_si32 keyID, r_bool press )
		{
			RDEBUG_ASSERT ( ( keyID >= 0 ) && ( keyID < DeviceControllKeys_Num ) );
			key[keyID] = press;
		}
		//! アナログ入力
		/*!
		*/
		void DeviceControllIn :: setAnalog ( r_si32 analogID, r_f32 val )
		{
			RDEBUG_ASSERT ( ( analogID >= 0 ) && ( analogID < DeviceControllKeyAnalog_Num ) );
			analog[analogID] = val;
		}
		//! 2Dアナログ入力
		/*!
		*/
		void DeviceControllIn :: setAnalog2D ( r_si32 analog2dID, const Analog2D& val )
		{
			RDEBUG_ASSERT ( ( analog2dID >= 0 ) && ( analog2dID < DeviceControllKeyAnalog2D_Num ) );

			analog2D[analog2dID] = val;
		}
		//! スクリーン入力
		/*!
		*/
		void DeviceControllIn :: setScreen ( const Screen& val )
		{
			screen = val;
		}

		void DeviceControllIn :: setIsConnect ( r_bool enable )
		{
			Device::setIsConnect ( enable );
		}
	}
}
